package com.itheima.night;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings(value = "all")
public class Demo03 {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("day18\\XML\\Students02.xml"));
        Element rootElement = document.getRootElement();
        List<Element> studentElements = rootElement.elements("Student");

        ArrayList<Student> studentList = new ArrayList<>();

        for (Element element : studentElements) {
            Attribute attribute = element.attribute("id");
            String id = attribute.getValue();

            Element nameElement = element.element("name");
            String name = nameElement.getText();

            Element ageElement = element.element("age");
            int age = Integer.parseInt(ageElement.getText());

            Student s = new Student(id,name,age);
            studentList.add(s);
        }

        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}
